package com.examples.gateway.helper.filter;

import com.examples.core.req.ExamHeader;
import com.examples.gateway.common.CheckState;
import com.examples.gateway.entity.SystemPermission;
import com.examples.gateway.helper.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 用户登录后即可访问
 * @author Dai YaLu
 */
@Slf4j
public class LoginAccessRequestFilter extends AbstractGlobalHelperFilter{
    @Override
    public Mono<Void> doFilter(ServerWebExchange exchange, GatewayFilterChain chain) {
        SystemPermission permission = exchange.getAttribute(Constants.EXAMPLES_GATEWAY_CHECK_PERMISSION);
        ExamHeader eh = exchange.getAttribute(Constants.EXAMPLES_GATEWAY_USER_DETAIL);
        if (null != permission && permission.needLoginAccess() && null != eh) {
            exchange.getAttributes().put(Constants.EXAMPLES_GATEWAY_CHECK_STATE, CheckState.SUCCESS_LOGIN_ACCESS);
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 80;
    }
}
